/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.rate.gift.service.impl;

import org.springblade.common.enums.rate.GiftIOTypeEnum;
import org.springblade.common.enums.rate.YesNoEnum;
import org.springblade.common.utils.CommonUtil;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.modules.rate.gift.entity.Gift;
import org.springblade.modules.rate.gift.entity.GiftPurchaseReturn;
import org.springblade.modules.rate.gift.service.IGiftService;
import org.springblade.modules.rate.gift.vo.GiftPurchaseReturnVO;
import org.springblade.modules.rate.gift.mapper.GiftPurchaseReturnMapper;
import org.springblade.modules.rate.gift.service.IGiftPurchaseReturnService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;

/**
 * 礼物采购表 服务实现类
 *
 * @author Blade
 * @since 2021-09-20
 */
@Service
public class GiftPurchaseReturnServiceImpl extends BaseServiceImpl<GiftPurchaseReturnMapper, GiftPurchaseReturn> implements IGiftPurchaseReturnService {

	@Autowired
	private IGiftService giftService;
	@Autowired
	@Lazy
	private IUserService userService;

	@Override
	public IPage<GiftPurchaseReturnVO> selectGiftPurchaseReturnPage(IPage<GiftPurchaseReturnVO> page, GiftPurchaseReturnVO giftPurchaseReturn) {
		return page.setRecords(baseMapper.selectGiftPurchaseReturnPage(page, giftPurchaseReturn));
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean submit(GiftPurchaseReturn giftPurchaseReturn) {
		try {
			boolean leaderFlag = false;
			BladeUser authUser = AuthUtil.getUser();
			User user = userService.getById(authUser.getUserId());
			if (user.getLeaderFlag()==YesNoEnum.YES.getKey()) {
				leaderFlag = true;
			}
			Gift giftQ = new Gift();
			giftQ.setCode(giftPurchaseReturn.getGiftCode());
			Gift gift = giftService.getOne(Condition.getQueryWrapper(giftQ).last("limit 1"));
			Integer number = gift.getNumber();
			if (giftPurchaseReturn.getType().equals(GiftIOTypeEnum.PURCHASE.getKey())) {
				number += giftPurchaseReturn.getNumber();
			}else {
				number -= giftPurchaseReturn.getNumber();
			}
			gift.setNumber(number);
			giftService.updateById(gift);
			this.saveOrUpdate(giftPurchaseReturn);

		} catch (Exception e) {
			log.error(e.getMessage());
			throw new ServiceException("提交失败");
		}
		return true;
	}
}
